﻿using Microsoft.Extensions.Hosting;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp.Infrastructure.TaskFramework
{
    public class TaskHostedService : BackgroundService
    {
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            ServicePointManager.DefaultConnectionLimit = 512;

            try
            {
                TaskManager taskManager = new TaskManager(TaskStarter.Init());
                taskManager.Start();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.Read();
            static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                Util.ConsoleHelper.WriteErrorLine("未知异常:" + (e.ExceptionObject as Exception).Message);
            }

            return Task.CompletedTask;
        }
    }
}
